#include "KPF_ListenerSession.h"

#include "log.h"

#include <iostream>
using namespace std;

KPF_ListenerSession::KPF_ListenerSession( const int8_t *pIP, const int16u_t iPort, const int16u_t iLocalPort )
	: m_IP( (char *)pIP ), m_iPort(iPort), m_iLocalPort(iLocalPort)
{
	CSessionParam stUDPSessionParam;
	
	int8_t pLocalIP[64] = { 0x00, };
	
	net_get_local_ip( pLocalIP, sizeof(pLocalIP) );
	
	memset( &stUDPSessionParam, 0x00, sizeof(stUDPSessionParam) );
	memcpy( stUDPSessionParam.pIP, (char *)pIP, strlen((char *)pIP) + 1 );
	memcpy( stUDPSessionParam.pLocalIP, (char *)pLocalIP, strlen((char *)pLocalIP) + 1 );
	stUDPSessionParam.iPort = iPort;
	stUDPSessionParam.iLocalPort = iLocalPort;
	
	cout<<m_IP<<endl;
	cout<<m_iPort<<endl;
	cout<<m_iLocalPort<<endl;
	
	log_print( (int8_t *)"start to create session............" );
	pm_Session = create_session( SESSION_TYPE_MULTICAST_LISTENER, &stUDPSessionParam );
	if ( pm_Session )
	{
		
		if ( set_session_business( pm_Session, onSessionBusiness, this ) >= 0 )
		{
			if ( set_session_event( pm_Session, onSessionEvent ) >= 0 )
			{
				log_print( (int8_t *)"create udp session ok................" );

			}
		}
	}
	else 
		log_print( (int8_t *)"create udp session failed????????????????????????" );
		
}
	
KPF_ListenerSession::~KPF_ListenerSession( void )
{
	
}
	
int32_t KPF_ListenerSession::handle_input( const KPF_Socket *pSocket, const int8u_t *pData, const int32_t iDataLen )
{
	int32_t iRetCode = -1;
	
	if ( pData && iDataLen > 0 )
	{
		fwrite( pData, 1, iDataLen, stdout );
		fflush( stdout );
		
		iRetCode = 0;
	}
	
	return iRetCode;
}

int32_t KPF_ListenerSession::handle_output( const KPF_Socket *pSocket, const int8u_t *pData, const int32_t iDataLen )
{
	int32_t iRetCode = -1;
	
	return iRetCode;
}
	
int32_t KPF_ListenerSession::handle_event( const KPF_Event &eEvent )
{
	int32_t iRetCode = -1;
	
	return iRetCode;
}
